package com.woniuxy.propertyjava.controller;


import com.woniuxy.propertyjava.entity.*;
import com.woniuxy.propertyjava.service.StaffService;
import com.woniuxy.propertyjava.service.StaffimageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;



/**
 * Created by IntelliJ IDEA.
 *
 * @Author : author
 * @create 2022/7/4 11:20
 */
@RestController
public class StaffController {
    @Autowired
    StaffService staffService;

    @Autowired
    StaffimageService staffimageService;

    @PostMapping("/staff/{pageIndex}/{pageSize}")
    public ResponseResult<PageBean<Staff>> getStaffsByPage(@PathVariable("pageIndex") int pageIndex,
                                                           @PathVariable("pageSize") int pageSize,
                                                           @RequestBody Staff staff) {
        PageBean<Staff> pageBean = staffService.getStaffPageBean(staff, pageIndex, pageSize);
        ResponseResult<PageBean<Staff>> responseResult = new ResponseResult<PageBean<Staff>>(200, "ok", pageBean);
        return responseResult;
    }

    @PostMapping("/staff")
    public ResponseResult addStaff(@RequestBody Staff staff) {
        staffService.inserStaff(staff);
        int staffid = staffService.getStaffIdByName(staff.getStaffname());
        Staffimage staffimage = new Staffimage();
        staffimage.setImageid(0);
        staffimage.setStaffid(staffid);
        staffimage.setImagesrc("http://localhost:8080/images/b.png");
        staffimageService.inserStaffimage(staffimage);
        ResponseResult responseResult = new ResponseResult<>(200, "添加成功", null);
        return responseResult;
    }

    @PutMapping("/staff")
    public ResponseResult changeStaff(@RequestBody Staff staff) {
        staffService.updateStaff(staff);
        ResponseResult responseResult = new ResponseResult<>(200, "修改成功", null);
        return responseResult;
    }

    @DeleteMapping("/staff/{staffId}")
    public ResponseResult reduceStaff(@PathVariable("staffId") int staffId) {
        staffService.deleteStaff(staffId);
        ResponseResult responseResult = new ResponseResult<>(200, "删除成功", null);
        return responseResult;
    }

    @GetMapping("/staffid")
    public  ResponseResult queryStaffByID(Integer staffid){
        Staff staff = staffService.queryById(staffid);
        ResponseResult responseResult=new ResponseResult(200,"ok",staff);
        return responseResult;
    }
}
